import requests
from email.mime import image
import json
from sqlite3 import Timestamp
from time import sleep
import tkinter
import uuid
import requests
import base64
import matplotlib.pyplot as plt
from matplotlib.image import imread
from matplotlib.pyplot import MultipleLocator
import random
import time
device_id = str(uuid.uuid4()).replace('-', '')[:16]
# device_id = '2E753979-F024-44EE-82C5-5697499A0F0E'

request_api_url = 'https://zwfw.hebei.gov.cn/v5/toupbgsp/h5_vote/index.php/Index/requestApi'

request_api_body = {'api':'gsp/uc00035','device_id':device_id}

request_api_res = requests.post(url=request_api_url,data=request_api_body)
 

dict_json = json.loads(request_api_res.content)

dict_json = dict_json['data']
capcode = ''
if 'backImage' in dict_json:
    print(dict_json)
    capcode = dict_json['capcode']
    img_data = base64.b64decode(dict_json['backImage'])
    img_slide_data = base64.b64decode(dict_json['slidingImage'])
    with open('001.jpg', 'wb') as f:
      f.write(img_data)
    with open('002.png', 'wb') as f:
      f.write(img_slide_data)
    print('success')

x_major_locator = MultipleLocator(15)
y_major_locator = MultipleLocator(15)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)

img = imread('001.jpg')
plt.imshow(img)
plt.show()

present = int(input('请输入x轴坐标'))

vote_url = 'https://zwfw.hebei.gov.cn//v5/toupbgsp/h5_vote/index.php/Elect/voteElect?option_ids=91&elect_id=6&device_id=' +  device_id +'&callback=__jp24'

slider_data = {}
slider_data['capcode'] = capcode
slider_data['xpos'] = present
slider_data['channel'] = '1'
mtrace = []
x = 60
for i in range(0,10):
    sleep(0.02)
    y = random.randint(520,540)
    x += int(present/10)
    t = time.time()
    time_stamp = int(round(t * 1000))
    temp_str = str(x) + "," + str(y) + "," + str(time_stamp)
    if i==0 :
        temp_str = str(x) + "," + str(y) + "," + "{% mock 'timestamp' %}"
    elif i == 9:
        temp_str = str(present+60) + "," + str(y) + "," + str(time_stamp)
    mtrace.append(temp_str)
slider_data["mtrace"] = mtrace

json_data = json.dumps(slider_data)

quary_url = vote_url + "&slider_data=" + json_data

vote_r = requests.get(quary_url)

print(vote_r.text)

for x in range(9):
    sleep(2)
    vote_r = requests.get(vote_url) 
    print(vote_r.text)

